package org.github.manx98.jlibhac.tools.fssystem.ncautils;

import org.github.manx98.jlibhac.common.math.Uint16;
import org.github.manx98.jlibhac.common.math.Uint32;

/**
 * @author Manx98
 * @version 1.0
 * @since 2022/10/6 18:12
 */
public class TitleVersion {
    public Uint32 Version;
    public int Major;
    public int Minor;
    public int Patch;
    public int Revision;

    public TitleVersion(Uint32 version, boolean isSystemTitle) {
        Version = version;

        if (isSystemTitle) {
            Revision = version.and(new Uint32(((1 << 16) - 1))).intValue();
            Patch = version.shiftRight(16).and(new Uint32((1 << 4) - 1)).intValue();
            Minor = version.shiftRight(20).and(new Uint32((1 << 6) - 1)).intValue();
            Major = version.shiftRight(26).and(new Uint32((1 << 6) - 1)).intValue();
        } else {
            Revision = version.intValue() & 255;
            Patch = version.shiftRight(8).intValue() & 255;
            Minor = version.shiftRight(16).intValue() & 255;
            Major = version.shiftRight(24).intValue() & 255;
        }
    }

    public String toString() {
        return String.format("%d.%d.%d.%d", Major, Minor, Patch, Revision);
    }
}
